package com.vr.xuecheng.base.response;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author huzhe
 * @date 2025/02/20
 * 统一结果返回类
 */
@Data
@AllArgsConstructor
public class Result<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 6851696556104605488L;

    private static final Integer SUCCESS_CODE = 0;
    private static final String SUCCESS_MESSAGE = "success";
    private static final String FAIL_MESSAGE = "error";

    private Integer code;
    private String message;
    private T result;

    public static <T> Result<T> ok(T result) {
        return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE, result);
    }
    public static <T> Result<T> ok(String message,T result) {
        return new Result<>(SUCCESS_CODE, message, result);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<T>(code, message, null);
    }
    public static <T> Result<T> error(T result, String message) {
        return new Result<T>(1,message,result);
    }
    public static <T> Result<T> toResult(int row) {
        return row>0?ok("操作成功",null):error(0,"操作失败");
    }
    public static <T> Result<T> toResult(boolean isSuccess) {
        return isSuccess?ok("操作成功",null):error(0,"操作失败");
    }
    public static <T> Result<T> toResult(boolean isSuccess,T result) {
        return isSuccess?ok("成功",result):error(result,"失败");
    }
    private static <T> boolean isSuccess(Result<T> result) {
        return SUCCESS_CODE.equals(result.getCode());
    }
}
